<?php
/**
 * @copyright © 2022 by 技术先锋 All rights reserved
 * @created   by PhpStorm.
 * @author    starsphp
 * @date      2023/3/29
 * @time      18:31
 */
declare (strict_types=1);

namespace utils\workerman;


use think\Cache;
use think\Config;
use think\Console;
use think\Cookie;
use think\Db;
use think\Env;
use think\Event;
use think\event\RouteLoaded;
use think\Lang;
use think\Log;
use think\Middleware;
use think\Response;
use think\Route;
use think\Session;
use think\Validate;
use think\View;
use utils\workerman\Http;


class App extends \think\App
{
    protected $bind = [
        'app'                     => \think\App::class,
        'cache'                   => Cache::class,
        'config'                  => Config::class,
        'console'                 => Console::class,
        'cookie'                  => Cookie::class,
        'db'                      => Db::class,
        'env'                     => Env::class,
        'event'                   => Event::class,
        'http'                    => Http::class, // Change think\Http to Http
        'lang'                    => Lang::class,
        'log'                     => Log::class,
        'middleware'              => Middleware::class,
        'request'                 => Request::class,
        'response'                => Response::class,
        'route'                   => Route::class,
        'session'                 => Session::class,
        'validate'                => Validate::class,
        'view'                    => View::class,
        'think\DbManager'         => Db::class,
        'think\LogManager'        => Log::class,
        'think\CacheManager'      => Cache::class,
        'Psr\Log\LoggerInterface' => Log::class,
    ];
    
    /**
     * 记录每个http计算运行时间和使用内存
     *
     * @interface init
     * @return void
     * @author    starsphp
     * @date      2023/3/29
     * @time      17:52
     */
    public function reinitialize(): void
    {
        if ($this->initialized) {
            $this->beginTime = microtime(true);
            $this->beginMem  = memory_get_usage();
        }
    
        // 加载变量环境 .env 文件
        $this->loadEnv();
        // 获取 变量配置文件 后缀  在 .env 文件中，
        // 若没有 ENV_NAME 配置值 则默认后缀为 default  （.env.default）
        $envName = \think\facade\Env::get('ENV_NAME', 'default');
        // 设置环境变量配置文件 动态
        $this->setEnvName($envName);
        $this->loadEnv($envName);
    }
    
}
